﻿//   StoredObject.cs
//   Copyright (c) 2012 Eswar Prakash
// 
//    This library is free software; you can redistribute it and/or modify  
//    it  under the terms of the GNU Lesser General Public License version  
//    2.1 as published by the Free Software Foundation.                     
//                                                                          
//    This library is distributed in the hope that it will be useful, but   
//    WITHOUT ANY WARRANTY; without even the implied warranty of            
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     
//    Lesser General Public License for more details.                       
//                                                                          
//    You should have received a copy of the GNU Lesser General Public      
//    License along with this library; if not, write to the Free Software   
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  
//    USA
namespace CKSAS.AppStorage
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// Represents the base class for all objects stored using AppStorage service
    /// </summary>
    [Serializable]
    public abstract class StoredObject
    {
        /// <summary>
        /// Private member variable used to hold a reference to the manager
        /// </summary>
        [NonSerialized]
        private AppStorageManager manager = null;

        /// <summary>
        /// Private member variable used to hold the status of the object
        /// </summary>
        [NonSerialized]
        private StoredObjectStatus status = StoredObjectStatus.Offline;

        /// <summary>
        /// Private member variable used to hold the login name of the modifier of the object
        /// </summary>
        [NonSerialized]
        private string modifiedBy = String.Empty;

        /// <summary>
        /// Private member variable used to hold the login name of the creator of the object
        /// </summary>
        [NonSerialized]
        private string createdBy = String.Empty;

        /// <summary>
        /// Private member variable used to hold the date and time when the object was last modified
        /// </summary>
        [NonSerialized]
        private DateTime modifiedDate = DateTime.MinValue;

        /// <summary>
        /// Private member variable used to hold the date and time when the object was created
        /// </summary>
        [NonSerialized]
        private DateTime createdDate = DateTime.MinValue;

        /// <summary>
        /// Private member variable used to hold the name of the object
        /// </summary>
        private string name = String.Empty;

        /// <summary>
        /// Initializes a new instance of the <see cref="StoredObject"/> class.
        /// </summary>
        /// <param name="name">The name of the object.</param>
        /// <param name="manager">The manager used to update and create the object in the storage.</param>
        protected StoredObject(string name, AppStorageManager manager)
        {
            this.Name = name;
            this.Manager = manager;
            this.Status = StoredObjectStatus.New;
        }

        /// <summary>
        /// Gets or sets the name of the object in the store
        /// </summary>
        public string Name
        {
            get { return this.name; }

            set { this.name = value; }
        }

        /// <summary>
        /// Gets or sets the status of the object in the storage
        /// </summary>
        public StoredObjectStatus Status
        {
            get { return this.status; }

            internal set { this.status = value; }
        }

        /// <summary>
        /// Gets or sets the manager connected to this object
        /// </summary>
        internal AppStorageManager Manager
        {
            get { return this.manager; }

            set { this.manager = value; }
        }

        /// <summary>
        /// Gets or sets the login name of the user who created this object.
        /// </summary>
        public string CreatedBy
        {
            get { return this.createdBy; }
            
            internal set { this.createdBy = value; }
        }

        /// <summary>
        /// Gets or sets the date the object was created.
        /// </summary>
        public DateTime CreatedDate
        {
            get { return this.createdDate; }

            internal set { this.createdDate = value; }
        }

        /// <summary>
        /// Gets or sets the login name of the user who last modified this object.
        /// </summary>
        public string ModifiedBy
        {
            get { return this.modifiedBy; }

            internal set { this.modifiedBy = value; }
        }

        /// <summary>
        /// Gets or sets the date the object was last modified.
        /// </summary>
        public DateTime ModifiedDate
        {
            get { return this.modifiedDate; }

            internal set { this.modifiedDate = value; }
        }

        /// <summary>
        /// Updates this instance in the storage.
        /// </summary>
        public virtual void Update()
        {
            if (this.Status == StoredObjectStatus.Offline)
            {
                throw new InvalidOperationException("The object is not in a valid state to support this operation");
            }

            this.Manager.SetObject(this);
        }

        /// <summary>
        /// Deletes this object from the storage
        /// </summary>
        public virtual void Delete()
        {
            if (this.Status == StoredObjectStatus.Offline)
            {
                throw new InvalidOperationException("The object is not in a valid state to support this operation");
            }

            this.Manager.DeleteObject(this);
        }
    }
}